package com.zz.service;

import com.zz.model.ImportData;
import com.zz.utils.DB;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: zhangzhen
 * DATE: Created in 2018/8/16 16:33
 * Description:
 */
public class ImportDataService {
    /* 查询导入列表信息*/
    public List<ImportData> list(int currentPage, int pageSize, String sort, String order) {
        Connection conn = DB.createConn();
        String sql = "select * from t_importdata where 1=1";
        if(StringUtils.isNotBlank(sort)) {
            sql += "order by" + sort;
        }
        if(StringUtils.isNotBlank(order)) {
            sql += " " + order;
        }
        sql += " limit " + (currentPage - 1)*pageSize + "," + pageSize;
        PreparedStatement ps = DB.prepare(conn, sql);
        List<ImportData> importDatas = new ArrayList<ImportData>();
        try {
            ResultSet rs = ps.executeQuery();
            ImportData i = null;
            while(rs.next()) {
                i = new ImportData();
                i.setImportid(rs.getString("importid"));
                i.setImportDataType(rs.getString("importdatatype"));
                i.setImportData(rs.getString("importdata"));
                i.setImportStatus(rs.getString("importstatus"));
                i.setHandleStatus(rs.getString("handlestatus"));
                i.setHandleData(rs.getString("handledata"));
                importDatas.add(i);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        DB.close(ps);
        DB.close(conn);
        return importDatas;

    }
}
